package com.ruoyi.project.mahjong.service.impl;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.http.HttpUtilsBody;
import com.ruoyi.framework.aspectj.lang.annotation.Excel;
import com.ruoyi.framework.redis.RedisCache;
import com.ruoyi.project.mahjong.domain.DeskIng;
import com.ruoyi.project.mahjong.domain.TOpenTable;
import com.ruoyi.project.mahjong.domain.enu.DeskStatusEnum;
import com.ruoyi.project.mahjong.domain.enu.TablenumberState;
import com.ruoyi.project.mahjong.domain.uyil.DeskUtils;
import com.ruoyi.project.mahjong.domain.vo.TableDianYuanVo;
import com.ruoyi.project.mahjong.domain.vo.TableVo;
import com.ruoyi.project.mahjong.mapper.DeskIngMapper;
import com.ruoyi.project.mahjong.mapper.TOpenTableMapper;
import com.ruoyi.project.mahjong.service.IDeskIngService;
import com.ruoyi.project.mahjong.service.ITOpenTableService;
import com.ruoyi.project.system.domain.SysUser;
import org.apache.velocity.app.event.implement.EscapeXmlReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mahjong.mapper.TTablenumberMapper;
import com.ruoyi.project.mahjong.domain.TTablenumber;
import com.ruoyi.project.mahjong.service.ITTablenumberService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 桌号Service业务层处理
 *
 * @author ruoyi
 * @date 2023-07-18
 */
@Service
public class TTablenumberServiceImpl implements ITTablenumberService {
    @Autowired
    private TTablenumberMapper tTablenumberMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private DeskIngMapper deskIngMapper;
    @Autowired
    private TOpenTableMapper tOpenTableMapper;

    /**
     * 查询桌号
     *
     * @param id 桌号ID
     * @return 桌号
     */
    @Override
    public TTablenumber selectTTablenumberById(Long id) {
        return tTablenumberMapper.selectTTablenumberById(id);
    }

    /**
     * 查询桌号列表
     *
     * @param tTablenumber 桌号
     * @return 桌号
     */
    @Override
    public List<TTablenumber> selectTTablenumberList(TTablenumber tTablenumber) {
        return tTablenumberMapper.selectTTablenumberList(tTablenumber);
    }

    /**
     * 新增桌号
     *
     * @param tTablenumber 桌号
     * @return 结果
     */
    @Override
    public int insertTTablenumber(TTablenumber tTablenumber) {
        tTablenumber.setCreateTime(DateUtils.getNowDate());
        return tTablenumberMapper.insertTTablenumber(tTablenumber);
    }

    /**
     * 修改桌号
     *
     * @param tTablenumber 桌号
     * @return 结果
     */
    @Override
    public int updateTTablenumber(TTablenumber tTablenumber) {
        tTablenumber.setUpdateTime(DateUtils.getNowDate());
        return tTablenumberMapper.updateTTablenumber(tTablenumber);
    }

    /**
     * 批量删除桌号
     *
     * @param ids 需要删除的桌号ID
     * @return 结果
     */
    @Override
    public int deleteTTablenumberByIds(Long[] ids) {
        return tTablenumberMapper.deleteTTablenumberByIds(ids);
    }

    /**
     * 删除桌号信息
     *
     * @param id 桌号ID
     * @return 结果
     */
    @Override
    public int deleteTTablenumberById(Long id) {
        return tTablenumberMapper.deleteTTablenumberById(id);
    }

    @Override
    public List<TTablenumber> getzchooseilist(TTablenumber tTablenumber) {
        return tTablenumberMapper.getzchooseilist(tTablenumber);
    }

    @Override
    public List<TTablenumber> getpoolist() {
        return tTablenumberMapper.getpoolist();
    }

    @Override
    public List<TableDianYuanVo> selectTableListByShopIdAndRoomId(Long shopId, Long roomId) {
        return tTablenumberMapper.selectTableListByShopIdAndRoomId(shopId, roomId);
    }

    @Override
    public Integer updateTableDianYuanStatus(Long tableId, Long status, Long shopId, Long oneStatus) {
        Integer i;
        if (status == 1 || status == 2) {
            i = tTablenumberMapper.updateAllTableDianYuanStatus(status, shopId);
        } else {
            i = tTablenumberMapper.updateTableDianYuanStatus(tableId, shopId, oneStatus);

        }
        if (i == 0) {
            throw new BaseException("控制失败电源,请重试");
        }

        return i;
    }

    @Override
    public List<TTablenumber> selectTableListByShopId(Long shopId) {
        return tTablenumberMapper.selectTableListByShopId(shopId);
    }

    @Override
    public List<TTablenumber> selectAllTableListByShopId(Long shopId) {
        return tTablenumberMapper.selectAllTableListByShopId(shopId);
    }

    @Override
    public void openEnergize(Long tableId) {
        String result = redisCache.getCacheObject(Constants.SHOP_POWER_TOKEN);
        //桌子通电操作
        TTablenumber tTablenumber = tTablenumberMapper.selectTTablenumberById(tableId);
        if (ObjectUtils.isNull(result)) {
            result = HttpUtilsBody.getToken(DeskUtils.creatUrl, null);
            redisCache.setCacheObject(Constants.SHOP_POWER_TOKEN, result, 50, TimeUnit.MINUTES);
        }
        Map params = new HashMap(1 << 1);//用于存放request body
        params.put("unid", tTablenumber.getSn());
        params.put("io", tTablenumber.getLo());
        //关闭

        tTablenumber.setIsEnergize("0");
        try {
            HttpUtilsBody.postHttpUrl(DeskUtils.openUrl, result, params);
        } catch (IOException e) {
            e.printStackTrace();
        }

        updateTTablenumber(tTablenumber);
    }

    @Override
    public void closeEnergize(Long tableId) {
        String result = redisCache.getCacheObject(Constants.SHOP_POWER_TOKEN);
        //桌子通电操作
        TTablenumber tTablenumber = tTablenumberMapper.selectTTablenumberById(tableId);
        if (ObjectUtils.isNull(result)) {
            result = HttpUtilsBody.getToken(DeskUtils.creatUrl, null);
            redisCache.setCacheObject(Constants.SHOP_POWER_TOKEN, result, 50, TimeUnit.MINUTES);
        }
        Map params = new HashMap(1 << 1);//用于存放request body
        params.put("unid", tTablenumber.getSn());
        params.put("io", tTablenumber.getLo());
        //关闭

        tTablenumber.setIsEnergize("0");
        try {
            HttpUtilsBody.postHttpUrl(DeskUtils.closeUrl, result, params);
        } catch (IOException e) {
            e.printStackTrace();
        }

        updateTTablenumber(tTablenumber);
    }

    @Override
    public List<TTablenumber> tableList(Long shopId) {
        TTablenumber tTablenumber = new TTablenumber();
        tTablenumber.setBelong(shopId.toString());
        List<TTablenumber> TTablenumber = tTablenumberMapper.selectTTablenumberList(tTablenumber);
        return TTablenumber;
    }

    /**
     * 重置桌子
     * @param ids
     * @return
     */
    @Override
    @Transactional()
    public int reset(Long ids) {
        //关闭桌子
        TTablenumber tTablenumber = tTablenumberMapper.selectTTablenumberById(ids);
        tTablenumber.setId(ids);
        tTablenumber.setStatus(TablenumberState.NO_LOCK.getValue());
        tTablenumber.setIsEnergize("0");
        tTablenumberMapper.updateTTablenumber(tTablenumber);
        //关闭开桌管理
        DeskIng deskIng= deskIngMapper.selectByOrderSn(tTablenumber.getOrderSn());
        deskIng.setStatus(DeskStatusEnum.OK_REFUND.getValue());
        deskIngMapper.updateDeskIng(deskIng);
        //关闭开桌详情
        tOpenTableMapper.updateByTableId(tTablenumber.getId());
        //桌子断电
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String result = redisCache.getCacheObject(Constants.SHOP_POWER_TOKEN);
        tTablenumber = selectTTablenumberById(tTablenumber.getId());
        if (ObjectUtils.isNull(result)) {
            result = HttpUtilsBody.getToken(DeskUtils.creatUrl, null);
            redisCache.setCacheObject(Constants.SHOP_POWER_TOKEN, result, 50, TimeUnit.MINUTES);
        }
        Map params = new HashMap(1 << 1);//用于存放request body
        params.put("unid", tTablenumber.getSn());
        params.put("io", tTablenumber.getLo());
        tTablenumber.setIsEnergize("0");
        try {
            HttpUtilsBody.postHttpUrl(DeskUtils.closeUrl, result, params);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
